package com.imooc.controller;

import com.imooc.com.imooc.entiy.User;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

/**
 * Created by Administrator on 2018/8/12.
 */
@Controller
@RequestMapping(value = "/admin")
public class FreemarkerController {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @RequestMapping(value = "/freemark")
    public String freemark() throws IOException, TemplateException {
        Configuration configuration=new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDirectoryForTemplateLoading(new File("H:\\ssm\\ssm\\src\\main\\webapp\\WEB-INF\\ftl"));
        configuration.setDefaultEncoding("UTF-8");
        Template template = configuration.getTemplate("hello.ftl");
        Map<Object, Object> map=new HashMap<>();
        map.put("hello", "hello world");
        Writer writer=new FileWriter(new File("H:\\ssm\\ssm\\src\\main\\webapp\\WEB-INF\\html\\hello.html"));
        template.process(map, writer);
        writer.close();
        return "index";
    }

    @RequestMapping(value = "/freemark/user")
    public String freemarkUser() throws IOException, TemplateException {
        Configuration configuration=new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDirectoryForTemplateLoading(new File("H:\\ssm\\ssm\\src\\main\\webapp\\WEB-INF\\ftl"));
        configuration.setDefaultEncoding("UTF-8");
        Template template = configuration.getTemplate("user.ftl");
        Map<Object, Object> map=new HashMap<>();
        List<User> lists=new ArrayList<>();
        User user=new User("123","23");
        User user1=new User("123","23");
        lists.add(user);
        lists.add(user1);
        map.put("lists", lists);
        map.put("date",new Date());
        map.put("val",456);
        Writer writer=new FileWriter(new File("H:\\ssm\\ssm\\src\\main\\webapp\\html\\user.html"));
        template.process(map, writer);
        writer.close();
        return "index";
    }
    @RequestMapping(value = "/freemark/spring")
    public String springFreemarkUser(HttpServletRequest request) {
        try {
            String path = request.getServletContext().getRealPath("html");
            File pathFile=new File(path);
            if (!pathFile.exists()){
                pathFile.mkdirs();
            }
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            String ftlName = "user.ftl";
            Template template = configuration.getTemplate(ftlName);
            Map<Object, Object> map=new HashMap<>();
            List<User> lists=new ArrayList<>();
            User user=new User("123","23");
            User user1=new User("123","23");
            lists.add(user);
            lists.add(user1);
            map.put("lists", lists);
            map.put("date",new Date());
            map.put("val",456);
            Writer writer=new FileWriter(new File(path,"user.html"));
            template.process(map, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }

}
